package com.changli05.communication;

//使用两个线程打印1-100。线程1，线程2交替打印
public class printNumber {
    public static void main(String[] args) {
        Print1 p1 = new Print1();
        Thread t1 = new Thread(p1,"线程1");
        Thread t2 = new Thread(p1,"线程2");
        t1.start();
         t2.start();
    }
}
class Print1 implements  Runnable{
    private static  int maxSize = 1;
    @Override
    public void run() {
        synchronized (this) {
//            Print1.class.notify();
            while(maxSize<=100){
                //解开锁
                notify();

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"->>>>"+maxSize);
                maxSize++;


                try {
                    //上锁
                    wait();
//                    Print1.class.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        }
    }
}
